package com.jay.tools.web;

import android.annotation.SuppressLint;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.webkit.WebView;

import com.jay.tools.base.BaseFragment;
import com.jay.tools.utils.LogUtil;
import com.jay.tools.web.event.LatteWebInterface;
import com.jay.tools.web.route.RouteKeys;
import com.jay.tools.web.webview.IWebVeiwInitializer;
import com.jay.tools.web.webview.MyWebView;

import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;

/**
 * Created by tan on 2017/9/22 022.
 */

public abstract class WebFragment extends BaseFragment {

    private WebView mWebView = null;
    // 防止内存泄漏
    private final ReferenceQueue<WebView> WEB_VIEW_QUEUE = new ReferenceQueue<>();

    private String mUrl = null;

    // 参照源码，如果WebView初始化完成则置为true，保证WebView可用
    private boolean mIsWebViewAbiable = false;
    // 设置这个保证webFragment以独立页面方式打开，避免携带底部导航栏
    private BaseFragment mTopFragment = null;

    public WebFragment() {

    }

    @Override
    protected void initPresenter() {

    }

    //  强制必须初始化接口
    public abstract IWebVeiwInitializer setInitializer();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mUrl = getArguments().getString(RouteKeys.URL);

        initWebView();
    }

    public void setTopFragment(BaseFragment fragment) {
        this.mTopFragment = fragment;
    }

    public BaseFragment getTopFragment() {
        if (null == mTopFragment) {
            mTopFragment = this;
        }
        return mTopFragment;
    }

    @SuppressLint("JavascriptInterface")
    private void initWebView() {
        if (null == mWebView) {
            final IWebVeiwInitializer initializer = setInitializer();
            if (null == initializer) {
                throw new NullPointerException("IWebVeiwInitializer is null");
            }

            // 在xml中直接使用WebView易造成内存泄漏，使用new WebView可以很大程度上避免
            final WeakReference<MyWebView> reference = new WeakReference<>(new MyWebView(getContext()), WEB_VIEW_QUEUE);

            mWebView = reference.get();
            initializer.initWebView(mWebView);

            mWebView.setWebViewClient(initializer.initWebViewClient());
            mWebView.setWebChromeClient(initializer.initWebChromeClient());

            // 获取注入JS的名称，目的是与原生应用进行交互
            final String jsName = "";
            mWebView.addJavascriptInterface(LatteWebInterface.create(this), jsName);

            mIsWebViewAbiable = true;
            return;
        }
        mWebView.removeAllViews();
        mWebView.destroy();
    }

    public WebView getWebView() {
        if (null == mWebView) {
            LogUtil.e("WebFragment", "WebView is null");
            return null;
        } else {
            return mIsWebViewAbiable ? mWebView : null;
        }
    }

    public String getUrl() {
        if (TextUtils.isEmpty(mUrl)) {
            throw new NullPointerException("WebView URL is null");
        } else {
            LogUtil.e("WebFragment", "getUrl:" + mUrl);
            return mUrl;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (null != mWebView) {
            mWebView.onResume();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (null != mWebView) {
            mWebView.onPause();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mIsWebViewAbiable = false;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != mWebView) {
            mWebView.removeAllViews();
            mWebView.destroy();
            mWebView = null;
        }
    }
}
